<?php
// +----------------------------------------------------------------------
// | Swagger路由
// +----------------------------------------------------------------------
use think\facade\App;
use think\facade\Route;

// Swagger-UI配置访问
Route::get('/swagger/api/:version/swagger.json', function ($version) {
	if (env('app.swagger_on') && is_dir(__DIR__ . '/../app/controller/' . $version)) {
		$openapi = \OpenApi\Generator::scan([
			__DIR__ . '/../app/controller/' . $version,
			__DIR__ . '/../app/model'
		]);
		header('Content-Type: application/json');
		$openapi->info->description .= '（ThinkPHP v' . App::version() . '）';
		echo $openapi->toJson();
	} else {
		echo '{
		    "openapi": "3.0.0",
		    "info": {
		        "title": "swagger-api",
		        "description": "未找到环境配置，或当前环境已关闭！",
		        "termsOfService": "",
		        "version": "1.0.0"
		    },
		    "paths": {},
		}';
	}
});
